#!/usr/bin/env perl
use strict;
use warnings;
use Test::More tests => 2;
use lib 'tests/search';
use AIR2TestUtils;    # sets lib path
use Data::Dump qw( dump );
use AIR2::Searcher::Engine;
use AIR2::Config;

SKIP: {

    if ( !AIR2TestUtils::search_env_ok() ) {
        skip "The search env does not look sane. Skipping all tests", 2; 
    }

    my $tz = AIR2::Config::get_tz();

    my $today = DateTime->from_epoch( epoch => time(), )
        ->set_time_zone($tz)->ymd('');
    my $thirty = DateTime->from_epoch( epoch => time() - ( 86400 * 30 ), )
        ->set_time_zone($tz)->ymd('');
    my $sixty = DateTime->from_epoch( epoch => time() - ( 86400 * 60 ), )
        ->set_time_zone($tz)->ymd('');
    my $ninety = DateTime->from_epoch( epoch => time() - ( 86400 * 90 ), )
        ->set_time_zone($tz)->ymd('');
    my $hundred = DateTime->from_epoch( epoch => time() - ( 86400 * 100 ), )
        ->set_time_zone($tz)->ymd('');
    my $raw_dates = [
        {   term  => $thirty,
            count => 10,
        },
        {   term  => $sixty,
            count => 20,
        },
        {   term  => $ninety,
            count => 40,
        },
        {   term  => $hundred,
            count => 70,
        },
    ];

    my $expected = [
        { count => 10, label => "last 30 days", term => "($thirty..$today)" },
        { count => 30, label => "last 60 days", term => "($sixty..$today)" },
        { count => 70, label => "last 90 days", term => "($ninety..$today)" },
        {   count => 70,
            label => "older than 90 days",
            term  => "(19000101.." . ( $ninety - 1 ) . ")",
        },
    ];

    ok( my $baked_dates
            = AIR2::Searcher::Engine->_do_date_range_summary($raw_dates),
        "date_range_summary"
    );

    #dump($baked_dates);

    is_deeply( $baked_dates, $expected, "date range facets baked correctly" );

}
